<template>
  <div class="login-page">
    <cp-nav-bar title="登录"></cp-nav-bar>
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '验证码登录' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{
          isPass ? '短信验证码登录' : '短信验证码登录'
        }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>

    <!-- form 表单 -->
    <VanForm autocomplete="off" ref="form" @submit="login">
      <VanField
        type="text"
        v-model="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
        name="mobile"
      ></VanField>
      <VanField
        :type="show ? 'text' : 'password'"
        v-model="password"
        placeholder="请输入密码"
        :rules="passwordRules"
        v-if="isPass"
      >
        <template #button>
          <cp-icon
            @click="show = !show"
            :name="`login-eye-${show ? 'on' : 'off'}`"
          ></cp-icon>
        </template>
      </VanField>
      <VanField
        v-model="code"
        :rules="codeRules"
        v-else
        placeholder="请输入验证码"
      >
        <template #button>
          <span @click="send" class="btn-send" :class="{ active: time > 0 }">{{
            time > 0 ? `${time}秒后发送验证码` : '发送验证码'
          }}</span>
        </template>
      </VanField>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </VanForm>

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onUnmounted } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import {
  showNotify,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { loginByCode, loginByPassword, sendMobileCode } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
const agree = ref(false)
const show = ref(false)
const mobile = ref('13230000079')
const password = ref('abc12345')
const store = useUserStore()
const route = useRoute()
const router = useRouter()

// 密码登录  | 验证码登录
const login = async () => {
  if (!agree.value) return showToast('请勾选用户协议')
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByCode(mobile.value, code.value)
  store.setUser(res.data)
  router.replace((route.query.returnUrl as string) || '/user')
  showSuccessToast('登录成功')
}

// 通过 isPass 来区分是密码登录还是验证码码登录
const isPass = ref(true)
const code = ref('')

// 验证码登录
const time = ref(0)
const form = ref<FormInstance | null>(null)
let timerId: number
const send = async () => {
  if (time.value > 0) return showToast('验证码已经发送')
  // 校验mobile
  await form.value?.validate('mobile') //await：程序出错阻止程序运行
  const res = await sendMobileCode(mobile.value, 'login')
  showNotify({
    type: 'primary',
    message: `验证码是${res.data.code}`,
    duration: 5000
  })

  //开启倒计时
  time.value = 60

  if (timerId) clearInterval(timerId)
  timerId = setInterval(() => {
    if (time.value <= 0) clearInterval(timerId)
    time.value--
  }, 1000)
}

onUnmounted(() => {
  clearInterval(timerId)
})
</script>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }

  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }

  &-other {
    margin-top: 60px;
    padding: 0 30px;

    .icon {
      display: flex;
      justify-content: center;

      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
